﻿Ext.ns('Ext.ux');

Ext.ux.HtmlViewer = Ext.extend(Ext.form.Field, {
			// private
			onRender : function(ct, position) {
				Ext.form.HtmlEditor.superclass.onRender.call(this, ct, position);
				this.el.dom.style.border = '0 none';
				this.el.dom.setAttribute('tabIndex', -1);
				this.el.addClass('x-hidden');
				if (Ext.isIE) { // fix IE 1px bogus margin
					this.el.applyStyles('margin-top:-1px;margin-bottom:-1px;');
				}
				this.wrap = this.el.wrap({
							cls : 'x-html-editor-wrap'
						});

				this.createIFrame();

				if (!this.width) {
					var sz = this.el.getSize();
					this.setSize(sz.width, this.height || sz.height);
				}
				this.resizeEl = this.positionEl = this.wrap;
			},

			createIFrame : function() {
				var iframe = document.createElement('iframe');
				iframe.name = Ext.id();
				iframe.frameBorder = '0';
				iframe.style.overflow = 'auto';

				this.wrap.dom.appendChild(iframe);
				this.iframe = iframe;
			},

			// private
			getBody : function() {
				var doc = this.getDoc();
				return doc.body || doc.documentElement;
			},

			// private
			getDoc : function() {
				return Ext.isIE ? this.getWin().document : (this.iframe.contentDocument || this
						.getWin().document);
			},

			// private
			getWin : function() {
				return Ext.isIE ? this.iframe.contentWindow : window.frames[this.iframe.name];
			},

			// private
			onResize : function(w, h) {
				Ext.ux.HtmlViewer.superclass.onResize.apply(this, arguments);
				if (this.el && this.iframe) {
					if (Ext.isNumber(w)) {
						var aw = w - this.wrap.getFrameWidth('lr');
						this.el.setWidth(aw);
						this.iframe.style.width = Math.max(aw, 0) + 'px';
					}
					if (Ext.isNumber(h)) {
						var ah = h - this.wrap.getFrameWidth('tb');
						this.el.setHeight(ah);
						this.iframe.style.height = Math.max(ah, 0) + 'px';
					}
				}
			},

			setValue : function(v) {
				Ext.ux.HtmlViewer.superclass.setValue.call(this, v);
				this.pushValue();
				return this;
			},

			pushValue : function() {
				if (this.el) {
					var v = this.el.dom.value, ss = this.el.getStyles('font-size', 'font-family',
							'background-color', 'color'), d = this.getDoc(), dbody = this.getBody();
					if (dbody) {
						dbody.innerHTML = '';
						d.close();
					}
					d.open();
					d.write('<base target="_blank" />');
					d.write(v);
					dbody = this.getBody();
					ss['background-attachment'] = 'fixed'; // w3c
					dbody.bgProperties = 'fixed'; // ie
					Ext.DomHelper.applyStyles(dbody, ss);
					dbody.style.marginTop = '5px';
					d.close();
				}
			},

			// private
			onDestroy : function() {
				if (this.rendered) {
					var doc = this.getDoc();
					if (doc) {
						try {
							Ext.EventManager.removeAll(doc);
							for (var prop in doc) {
								delete doc[prop];
							}
						} catch (e) {
						}
					}
					if (this.wrap) {
						this.wrap.dom.innerHTML = '';
						this.wrap.remove();
					}
				}

				if (this.el) {
					this.el.removeAllListeners();
					this.el.remove();
				}
				this.purgeListeners();
			}
		});
Ext.reg('htmlviewer', Ext.ux.HtmlViewer);